package com.zhangliang.oa.crud;

import com.zhangliang.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet()
public class DeptListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String contextPath = req.getContextPath();
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("    <meta charset='UTF-8'>");
        out.print("    <title>list</title>");
        out.print("<script type='text/javascript'>");
        out.print("            function del(dno){");
        out.print("        if(window.confirm('确认删除?')){");
        out.print("            document.location.href='"+contextPath+"/dept/delete?deptno='+dno;");
        out.print("        }");
        out.print("    }");
        out.print("</script>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1 align='center'>部门列表</h1>");
        out.print("<hr>");
        out.print("<table border='1px' width='50%' align='center'>");
        out.print("    <tr>");
        out.print("        <th>部门编号</th>");
        out.print("        <th>部门名称</th>");
        out.print("        <th>工作地点</th>");
        out.print("        <th>操作</th>");
        out.print("    </tr>");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept;";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                String deptno = rs.getString("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                out.print("    <tr align='center'>");
                out.print("        <td>"+deptno+"</td>");
                out.print("        <td>"+dname+"</td>");
                out.print("        <td>"+loc+"</td>");
                out.print("        <td>");
                out.print("            <a href='javascript:(0)' onclick='del("+deptno+")'>删除</a>");
                out.print("            <a href='"+contextPath+"/dept/edit?deptno="+deptno+"'>修改</a>");
                out.print("            <a href='"+contextPath+"/dept/select?deptno="+deptno+"'>查询</a>");
                out.print("        </td>");
                out.print("    </tr>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,rs);
        out.print("</table>");
        out.print("<a href='"+contextPath+"/add.html'>新增部门</a>");
        out.print("</body>");
        out.print("</html>");
    }
}
